package com.api.workflow.web;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.workflow.util.YNHttpUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YNFII010AAction extends BaseBean implements Action {


    private String gys;
    private String gsdm;
    private String hb;

    public String getGys() {
        return gys;
    }

    public void setGys(String gys) {
        this.gys = gys;
    }

    public String getGsdm() {
        return gsdm;
    }

    public void setGsdm(String gsdm) {
        this.gsdm = gsdm;
    }

    public String getHb() {
        return hb;
    }

    public void setHb(String hb) {
        this.hb = hb;
    }

    @Override
    public String execute(RequestInfo requestInfo) {


        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String workflowid = requestInfo.getWorkflowid();

        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableDBName = "formtable_main_" + formidAbs;

        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        //这里是存储了主表全部的数据  可自行修改存储结构
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }
        baseBean.writeLog("=======YNFII010AAction======requestid:" +requestid);
        baseBean.writeLog("=======YNFII010AAction======mainTableInfoMap:" + JSONObject.toJSONString(mainTableInfoMap));
        baseBean.writeLog("=======YNFII010AAction======gys:" + gys);
        baseBean.writeLog("=======YNFII010AAction======gsdm:" + gsdm);
        String hbzd=Util.null2String(hb);
        baseBean.writeLog("=======YNFII010AAction======hbzd:" + hbzd);
        if(hbzd.isEmpty()){
            hbzd="hb";
        }
        //明细表数据
        List<Map<String, Object>> detailTableMap = getMaps(requestInfo);
        baseBean.writeLog("=======YNFII010AAction======detailTableMap:" + JSONObject.toJSONString(detailTableMap));
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("LIFNR", mainTableInfoMap.get(gys));
            map.put("BUKRS", mainTableInfoMap.get(gsdm));
            map.put("WAERS", mainTableInfoMap.get(hbzd));
            JSONObject object = new JSONObject(map);
            baseBean.writeLog("YNFII010AAction==========object" + object.toJSONString());
            String post = YNHttpUtils.FII010A(object.toJSONString(), YNHttpUtils.token());
            baseBean.writeLog("YNFII010AAction==========post" + post);
            JSONObject jsonObject = JSONObject.parseObject(post);


            String STATUS = jsonObject.getString("STATUS");
            String MESSAGE = jsonObject.getString("MESSAGE");
            baseBean.writeLog("YNFII010AAction==========MESSAGE" + MESSAGE);


            if (!"S".equals(STATUS)) {
                return Action.SUCCESS;
//                requestInfo.getRequestManager()
//                        .setMessageid(requestInfo.getRequestid() + "OA-27");
//                requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:" + MESSAGE);
//                return Action.FAILURE_AND_CONTINUE;
            }

            JSONArray data = jsonObject.getJSONArray("DATA");
            JSONObject firstElement =  data.getJSONObject(0);
            firstElement.getString("LIFNR");
            firstElement.getString("WRBTR");
            firstElement.getString("ZGWRBTR");
            RecordSet recordSet = new RecordSet();

            String updateMainTableName = "update " + tableDBName + " set sapfhgsdm = ? , sapfhhb = ? ,  sapfhje=? , sapfhzgje = ?  where  requestid = ? ";
            recordSet.executeUpdate(updateMainTableName, firstElement.getString("BUKRS"), firstElement.getString("WAERS"), firstElement.getString("WRBTR"), firstElement.getString("ZGWRBTR"), requestid);
            baseBean.writeLog("YNFII010AAction==========updateMainTableName" + updateMainTableName);
        } catch (Exception e) {
            //捕获异常并记录日志
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid() + "OA-27");
            requestInfo.getRequestManager().setMessagecontent("OA接口异常" + e.toString());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;

    }

    /**
     * 提取明细表全部的数据
     *
     * @param requestInfo
     * @return
     */
    private static List<Map<String, Object>> getMaps(RequestInfo requestInfo) {
        DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
        //这里是存储了全部的明细表数据  可自行修改存储结构
        List<Map<String, Object>> detailTableMap = new ArrayList<>();
        if (detailTable.length > 0) {
            for (int i = 0; i < detailTable.length; i++) {
                DetailTable dt = detailTable[i];
                Row[] s = dt.getRow();
                for (int j = 0; j < s.length; j++) {
                    Row r = s[j];
                    Cell c[] = r.getCell();
                    Map<String, Object> objectObjectHashMap = new HashMap<>();
                    objectObjectHashMap.put("detailTableName", dt.getTableDBName());
                    for (int k = 0; k < c.length; k++) {
                        Cell c1 = c[k];
                        String name = c1.getName().toLowerCase();
                        String value = c1.getValue();
                        objectObjectHashMap.put(name, value);

                    }
                    detailTableMap.add(objectObjectHashMap);
                }
            }
        }
        return detailTableMap;
    }
}
